package com.east.sng.controller;

import com.east.sng.entity.param.TeacherParam;
import com.east.sng.entity.result.TeacherFind;
import com.east.sng.entity.result.TeacherInfo;
import com.east.sng.service.CourseService;
import com.east.sng.service.CourseTypeService;
import com.east.sng.service.TeacherService;
import com.east.sng.utils.EncryptDataUtil;
import com.east.sng.utils.ValidationUtil;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author east
 * @date: 2020/12/30
 * @Description:
 **/
@Controller
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private CourseTypeService courseTypeService;

    /**
     * 教师信息页面跳转
     * @param teacherName
     * @param teacherType
     * @param pageNum
     * @param model
     * @return
     */
    @RequiresPermissions("teacher:list")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(Integer teacherType, String teacherName, @RequestParam(name="flag",defaultValue="0")Integer flag,
                       @RequestParam(name="pn",defaultValue="1")Integer pageNum,
                       @RequestParam(name = "pSize", defaultValue = "5") Integer pageSize, Model model){
        Map<String, Object> params = new HashMap<>();
        params.put("teacherName",teacherName);
        params.put("teacherType",teacherType);
        params.put("flag",flag);
        model.addAttribute("page", PageHelper.startPage(pageNum, pageSize));
        List<TeacherInfo> list = this.teacherService.findTeachers(params);
        //给教师手机号加密
        list.forEach((item)-> {
            String encryptPhone = EncryptDataUtil.encryptPhone(item.getTeacherPhone());
            item.setTeacherPhone(encryptPhone);
        });
        model.addAttribute("teachers",list);
        model.addAttribute("courseTypes",this.courseTypeService.findCourseTypeColumns());
        return "teacher/list";
    }

    /**
     * 教师信息录入页面跳转
     * @param model
     * @return
     */
    @RequiresPermissions("teacher:add")
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String toAddTeacher(Model model){
        model.addAttribute("courseTypes",this.courseTypeService.findCourseTypeColumns());
        return "teacher/add";
    }

    /**
     * 教师信息录入
     * @param teacher
     * @param br
     * @return
     */
    @RequiresPermissions("teacher:add")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String addTeacher(@Valid TeacherParam teacher, BindingResult br){
        ValidationUtil.validateData(br);
        this.teacherService.addTeacher(teacher);
        return "success";
    }

    /**
     * 教师信息修改页面跳转
     * @param teacherId
     * @param model
     * @return
     */
    @RequiresPermissions(value = {"teacher:list","teacher:add"},logical = Logical.AND)
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public String toEditTeacher(Integer teacherId,Model model){
        TeacherFind teacher = this.teacherService.findTeacher(teacherId);
        model.addAttribute("teacher",teacher);
        model.addAttribute("courseTypes",this.courseTypeService.findCourseTypeColumns());
        return "teacher/edit";
    }

    /**
     * 教师信息修改
     * @param teacher
     * @param br
     * @return
     */
    @RequiresPermissions(value = {"teacher:list","teacher:add"},logical = Logical.AND)
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public String editTeacher(@Valid TeacherParam teacher, BindingResult br){
        ValidationUtil.validateData(br);
        this.teacherService.editTeacher(teacher);
        return "success";
    }

    /**
     * 教师信息删除
     * @param id
     * @return
     */
    @RequiresPermissions(value = {"teacher:list","teacher:add"},logical = Logical.AND)
    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @ResponseBody
    public String removeTeacher(Integer id){
        return this.teacherService.removeTeacher(id)?"success":"fail";
    }

    /**
     * 教师信息恢复
     * @param id
     * @return
     */
    @RequiresRoles("0")
    @RequiresPermissions(value = {"teacher:list","teacher:add"},logical = Logical.AND)
    @RequestMapping(value = "/recover",method = RequestMethod.POST)
    @ResponseBody
    public String recoverTeacher(Integer id){
        return this.teacherService.recoverTeacher(id)?"success":"fail";
    }

    /**
     * 教师信息批量删除
     * @param ids
     * @return
     */
    @RequiresPermissions(value = {"teacher:list","teacher:add"},logical = Logical.AND)
    @RequestMapping(value = "/removes",method = RequestMethod.POST)
    @ResponseBody
    public String removeTeachers(Integer[] ids){
        return this.teacherService.batchRemoveTeachers(Arrays.asList(ids))?"success":"fail";
    }

    /**
     * 教师信息批量恢复
     * @param ids
     * @return
     */
    @RequiresRoles("0")
    @RequiresPermissions(value = {"teacher:list","teacher:add"},logical = Logical.AND)
    @RequestMapping(value = "/recovers",method = RequestMethod.POST)
    @ResponseBody
    public String recoverTeachers(Integer[] ids){
        return this.teacherService.batchRecoverTeachers(Arrays.asList(ids))?"success":"fail";
    }

    /**
     * 根据专业查询价格、授课教室和教师
     * @param teacherType
     * @return
     */
    @RequiresPermissions("teacher:add")
    @RequestMapping(value = "/findClassroom",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> findClassroom(Integer teacherType){
        List<Map<String, Object>> crs = courseService.findClassrooms(teacherType);
        HashMap<String, Object> classrooms = new HashMap<>();
        classrooms.put("classrooms",crs);
        return classrooms;
    }

}
